package com.x.erp.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.x.erp.dao.InsuranceOrderDao;
import com.x.erp.dao.InsuranceOrderGoodsDao;
import com.x.erp.manage.InsuranceOrderManage;
import com.x.erp.model.InsuranceOrder;
import com.x.erp.model.InsuranceOrderGoods;
import com.x.erp.rpc.InsuranceOrderRpcService;
import com.x.framework.exception.BusinessException;
import com.x.framework.exception.ExceptionCode;
import com.x.framework.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 延保商品服务
 */
@Component
@Service(interfaceName = "com.x.erp.rpc.InsuranceOrderRpcService", timeout = 60000)
public class InsuranceOrderService extends BaseService implements InsuranceOrderRpcService {

	@Autowired
	private InsuranceOrderDao insuranceOrderDao; // 延保商品订单dao

	@Autowired
	private InsuranceOrderGoodsDao insuranceOrderGoodsDao; // 订单商品dao

	@Autowired
	private InsuranceOrderManage insuranceOrderManage; // 延保商品订单manage

	/**
	 * 获取延保商品订单列表
	 *
	 * @param insuranceOrder
	 * @return
	 */
	public List<InsuranceOrder> selectInsuranceOrderList(InsuranceOrder insuranceOrder) {
		try {
			List<InsuranceOrder> orderList = insuranceOrderDao.selectInsuranceOrderList(insuranceOrder);
			for (InsuranceOrder ordertemp : orderList) {
				// 查询订单商品信息
				if(ordertemp != null){
					setOrderGoodsList(ordertemp);
				}
			}
			return orderList;
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	// 查询订单商品信息
	private void setOrderGoodsList(InsuranceOrder ordertemp) throws Exception {
		InsuranceOrderGoods orderGoods = new InsuranceOrderGoods();
		orderGoods.setOrderId(ordertemp.getOrderId());
		List<InsuranceOrderGoods> orderGoodsList = insuranceOrderGoodsDao.selectInsuranceOrderGoodsList(orderGoods);
		ordertemp.setOrderGoodsList(orderGoodsList);
	}

	/**
	 * 获取延保商品订单数量
	 *
	 * @param insuranceOrder
	 * @return
	 */
	public int selectInsuranceOrderNum(InsuranceOrder insuranceOrder) {
		try {
			int res = insuranceOrderDao.selectInsuranceOrderNum(insuranceOrder);
			return res;
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}


	/**
	 * 获取单个延保商品订单信息
	 *
	 * @param insuranceOrder
	 * @return
	 */
	public InsuranceOrder selectInsuranceOrder(InsuranceOrder insuranceOrder) {
		try {
			InsuranceOrder ordertemp = insuranceOrderDao.selectInsuranceOrder(insuranceOrder);
			// 查询订单商品信息
			if(ordertemp != null){
				setOrderGoodsList(ordertemp);
			}
			return ordertemp;
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 新增延保商品订单信息
	 *
	 * @param insuranceOrder
	 * @return
	 */
	public InsuranceOrder insertInsuranceOrder(InsuranceOrder insuranceOrder) {
		try {
			return insuranceOrderManage.insertInsuranceOrder(insuranceOrder);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 删除延保商品订单信息
	 *
	 * @param insuranceOrder
	 * @return
	 */
	public int deleteInsuranceOrder(InsuranceOrder insuranceOrder) {
		try {
			return this.delete(insuranceOrder);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 修改延保商品订单信息
	 *
	 * @param insuranceOrder
	 * @return
	 */
	public int updateInsuranceOrder(InsuranceOrder insuranceOrder) {
		try {
			insuranceOrder.setOrderUpdateTime(new Date());
			return this.update(insuranceOrder);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

}
